package com.gopay.bis.pab.ebank.operation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.pab.ebank.PabQueryOrdersMessageRes;
import com.gopay.common.domain.bank.pab.ebank.PadQueryOrdersMessageReq;
import com.gopay.common.exception.GopayException;
import com.sdb.ebank.bean.data.AllOrderListReceive;
import com.sdb.ebank.bean.data.AllOrderListSend;
import com.sdb.payment.core.PaymentInterfaceUtil;

public class PabB2bQueryOrder {
	private PabTransformer pabTransformer;
	// 配置文件名
	private String merchantName;
	// 用户ID
	private String userId;
	// 密码
	private String password;
	
	private final static Logger logger = LoggerFactory.getLogger(PabB2bQueryOrder.class);

	/**
	 * 查询
	 * 
	 * @param BaseMessage
	 *            message
	 * @return ResSDBQueryOrdersMessage
	 * @throws GopayException 
	 */
	public PabQueryOrdersMessageRes query(BaseMessage message) throws GopayException {
		try {
			logger.error("------pab b2b userId------"+userId);
			PadQueryOrdersMessageReq req = getReqOrderMessage(message);
			BankLogUtils.printB2BQuerySendLog(message," PAB B2B query orderid:"+ req);
			AllOrderListSend orderListSend = pabTransformer.format(req);
			AllOrderListReceive rec = null;
			PaymentInterfaceUtil util = new PaymentInterfaceUtil(merchantName);
			util.login(userId, password);
			rec = util.getAllOrderList((AllOrderListSend) orderListSend);
			//可以不用登出  update  by lilili
			//util.logout(userId);
			PabQueryOrdersMessageRes orders = pabTransformer.parse(rec);
			BankLogUtils.printB2BQueryBlackLog(message, orders);
			return orders;
		} catch (Exception e) {
			BankLogUtils.printB2BQueryExceptionLog(message, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6036);
		}
	}

	private PadQueryOrdersMessageReq getReqOrderMessage(BaseMessage message) {
		PadQueryOrdersMessageReq req = new PadQueryOrdersMessageReq();
		// 对账开始时间设为当前交易日期的前一天
		java.util.Date dateInxTxnDt = getDate4Str(message.getOrderDate());// 内部交易日期Date
		java.util.Date beginDate = getConditionDate(Calendar.DATE, -1,
				dateInxTxnDt);
		java.sql.Date sqlBeginDate = new java.sql.Date(beginDate.getTime());
		// 对账开始时间设为当前交易日期
		java.sql.Date sqlEndDate = new java.sql.Date(dateInxTxnDt.getTime());
		req.setBeginDate(sqlBeginDate);
		req.setEndDate(sqlEndDate);
		req.setBeginTime("");
		req.setEndTime("");
		req.setOrderId(message.getOrderId());
		return req;
	}

	/**
	 * str转换为日期
	 * 
	 * @param strDate
	 * @return
	 * @throws ParseException
	 */
	private java.util.Date getDate4Str(String strDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		java.util.Date intDt = null;
		try {
			intDt = sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		return intDt;
	}

	/**
	 * 根据日历的规则，为给定的日历字段添加或减去指定的时间量
	 * 
	 * @param field
	 * @param amount
	 * @param date
	 *            给定时间
	 * @return
	 */
	private java.util.Date getConditionDate(int field, int amount,
			java.util.Date date) {
		Calendar calendar = Calendar.getInstance();// 默认日历
		calendar.setTime(date);// 设置时间
		calendar.add(field, amount);// 根据日历的规则，为给定的日历字段添加或减去指定的时间量
		java.util.Date conditionDate = calendar.getTime();// 获得时间
		return conditionDate;
	}

	public PabTransformer getPabTransformer() {
		return pabTransformer;
	}

	public void setPabTransformer(PabTransformer pabTransformer) {
		this.pabTransformer = pabTransformer;
	}

	public void setMerchantName(String merchantName) {
		this.merchantName = merchantName;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
